<?php

include ('../session.php');
include ('../db.php');
include ('../functions.php');
include ('config.php');

if ($action == 'add')
{
  print_r ($_POST);

  $c_raidid = intval ($raid);
  $c_itemname = mysql_clean ($name);
  $c_itemvalue = floatval ($points);

  $query = "select item_id
            from " . DKP_ITEMS . " as items
            where item_name = '$c_itemname'";
  $result = mysql_query ($query);

  $row = mysql_fetch_array ($result);

  if ($row)
  {
    $itemid = $row['item_id'];
  }
  else
  {
    $query = "select idvalue
              from ids
              where idid = " . ID_ITEM;
    $result = mysql_query ($query);

    $row = mysql_fetch_array ($result);

    $itemid = $row['idvalue'];
    $c_itemid = intval ($itemid);

    $query = "insert into " . DKP_ITEMS . "
              (item_id, item_name, item_value)
              values ($c_itemid, '$c_itemname', '$c_itemvalue')";
    $result = mysql_query ($query);

    $query = "update ids
              set idvalue = " . intval ($itemid + 1) . "
              where idid = " . ID_ITEM;
    $result = mysql_query ($query);

  }

  echo mysql_error ();

  foreach ($members as $key => $memberid)
  {
    $c_memberid = intval ($memberid);
  
    $query = "select idvalue
              from ids
              where idid = " . ID_DROP;
    $result = mysql_query ($query);

    $row = mysql_fetch_array ($result);

    $dropid = $row['idvalue'];
    $c_dropid = intval ($dropid);

    $c_itemid = intval ($itemid);

    $query = "insert into " . DKP_DROPS . "
              (drop_id, item_id, member_id, raid_id, item_value)
              values ($c_dropid, $c_itemid, $c_memberid, $c_raidid, $c_itemvalue)";
    $result = mysql_query ($query);

    echo mysql_error ();

    $query = "update ids
              set idvalue = " . intval ($dropid + 1) . "
              where idid = " . ID_DROP;
    $result = mysql_query ($query);

  }         
}
else if ($action == 'update')
{
}
else if ($action == 'remove')
{
}

$output = '';

$output .= "
<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">
<html>
  <head>
    <title>Panic Points - Add Drop</title>
    <link href=\"../general.css\" rel=\"stylesheet\" type=\"text/css\">
  </head>
  <body>";

include ('navigation.php');

$output .= "
    <script type=\"text/javascript\">
      function search_item (text)
      {
        if (!text)
        {
          return;
        }

        var regexp = new RegExp (text, 'i');

        var items = document.getElementById ('items');
        var length = items.length;

        for (var i = 0; i < length; i++)
        {
          if (regexp.test (items.options[i].text))
          {
            items.options[i].selected = true;

            break;
          }
        }
      }

      function update_item ()
      {
        var regexp = /(.+) \(([0-9]+\.[0-9]+)\)/;

        var items = document.getElementById ('items');

        var matches = regexp.exec (items.options[items.selectedIndex].text);

        document.getElementById ('points').value = matches[2];
        document.getElementById ('name').value = matches[1];
      }
    </script>";

$output .= "
    <form name=\"dropform\" action=\"adddrop.php\" method=\"POST\">
      <input type=\"hidden\" name=\"action\" id=\"action\" value=\"\">
      <table>
        <tr valign=\"top\">
          <td>";

$output .= "
            <table>
              <tr>
                <td>
                  <select name=\"members[]\" size=\"20\" style=\"width: 100px\" multiple>";
                
$query = "select member_id, member_name
          from " . DKP_MEMBERS . "
          order by member_name asc";
$result = mysql_query ($query);

while ($row = mysql_fetch_array ($result))
{
  $output .= "
                    <option value=\"" . $row['member_id'] . "\">" . $row['member_name'] . "</option>";
}

$output .= "
                  </select>
                </td>
              </tr>
            </table>";

$output .= "
          </td>
          <td>";
        
$output .= "
            <table>
              <tr>
                <td colspan=\"2\">select raid</td>
              </tr>
              <tr>
                <td colspan=\"2\" style=\"padding-bottom: 10px\">
                  <select name=\"raid\">";
                
$query = "select raid_id, event_name, location_name
          from " . DKP_RAIDS . " as raids
          inner join " . DKP_LOCATIONS . " as locations
          on locations.location_id = raids.location_id
          inner join " . DKP_EVENTS . " as events
          on events.event_id = raids.event_id
          order by raid_date desc";
$result = mysql_query ($query);

echo mysql_error ();

while ($row = mysql_fetch_array ($result))
{
  $output .= "
                    <option value=\"" . $row['raid_id'] . "\">" . $row['location_name'] . " - " . $row['event_name'] . "</option>";
}

$output .= "
                  </select>
                </td>
              </tr>
              <tr>
                <td colspan=\"2\" style=\"border-top: 1px solid #cccccc; padding-top: 10px\">existing item</td>
              </tr>
              <tr>
                <td colspan=\"2\"><input id=\"existing\" type=\"text\" onkeyup=\"search_item (this.value); update_item ()\"></td>
              </tr>
              <tr>
                <td colspan=\"2\" style=\"padding-bottom: 10px\">
                  <select name=\"item\" id=\"items\" onchange=\"update_item ()\">";

$query = "select item_id, item_name, item_value
          from " . DKP_ITEMS . "
          order by item_name asc";
$result = mysql_query ($query);

while ($row = mysql_fetch_array ($result))
{
  $output .= "
                    <option value=\"" . $row['item_id'] . "\">" . $row['item_name'] . " (" . $row['item_value'] . ")</option>";
}

$output .= "
                  </select>
                </td>
              </tr>
              <tr>
                <td colspan=\"2\" style=\"border-top: 1px solid #cccccc; padding-top: 10px\"><input type=\"text\" id=\"name\" name=\"name\"></td>
              </tr>
              <tr>
                <td style=\"padding-bottom: 10px\">points</td>
                <td style=\"padding-bottom: 10px\" ><input type=\"text\" name=\"points\" id=\"points\"></td>
              </tr>
              <tr>
                <td colspan=\"2\" style=\"border-top: 1px solid #cccccc; padding-top: 10px\">
                  <button onclick=\"document.getElementById ('action').value = 'add'; document.getElementById ('dropform').submit ()\">add</button>&nbsp;
                  <button onclick=\"document.getElementById ('action').value = 'update'; document.getElementById ('dropform').submit ()\">update</button>&nbsp;
                  <button onclick=\"document.getElementById ('action').value = 'remove'; document.getElementById ('dropform').submit ()\">remove</button>
               </td>
              </tr>
            </table>
          </form>";
        
$output .= "
        </td>
      </tr>
    </table>";

$output .= "
    <script type=\"text/javascript\">
      update_item ();
      document.getElementById ('existing').focus ();
    </script>
  </body>
</html>";

echo $output;

?>

